﻿using System;
using System.Collections.Generic;
using System.Text;
using Slippor.Common.PluginFramework;
using Slippor.Douban.Radio.Assist;

namespace Slippor.Douban.Radio.BaseForm
{
    class DoubanRadioHotkeyService:IHotkeyService
    {
        private IntPtr _hwnd;

        public IntPtr Handle
        {
            get { return _hwnd; }
            set { _hwnd = value; }
        }

        public DoubanRadioHotkeyService(IntPtr hwnd)
        {
            _hwnd = hwnd;
        }

        private Dictionary<int, IHotkeyExcutor> _commands = new Dictionary<int, IHotkeyExcutor>();

        public void Check(int msg)
        {
            foreach(int command in _commands.Keys)
            {
                if(msg == command)
                {
                    _commands[command].Excute();
                }
            }
        }

        #region IHotkeyService Members
        public void RegHotkey(int command, IHotkeyExcutor excutor)
        {
            _commands[command] = excutor;
            HotKey.RegisterHotKey(_hwnd, excutor.Hotkey, command);
        }

        public void RemoveHotkey(int command)
        {
            _commands.Remove(command);
            HotKey.UnregisterHotKey(_hwnd, command);
        }

        public void UpdateHotkey(String hotKey, int command)
        {
            if(_commands.ContainsKey(command))
            {
                HotKey.UnregisterHotKey(_hwnd, command);
            }
            _commands[command].Hotkey = hotKey;
            HotKey.RegisterHotKey(_hwnd, hotKey, command);
        }
        #endregion

        public void RemoveAll()
        {
            foreach (int command in _commands.Keys)
            {
                HotKey.UnregisterHotKey(_hwnd, command);
            }
        }

        public void RegAll()
        {
            foreach (int command in _commands.Keys)
            {
                HotKey.RegisterHotKey(_hwnd, _commands[command].Hotkey, command);
            }
        }
    }
}
